package com.aaa.outbound.controller;


import com.aaa.entity.outbound.Outorder;
import com.aaa.entity.outbound.Pick;
import com.aaa.entity.outbound.PickDetail;
import com.aaa.entity.outbound.PicktaskDetail;
import com.aaa.outbound.service.OutorderService;
import com.aaa.outbound.service.PickDetailService;
import com.aaa.outbound.service.PickService;
import com.aaa.vo.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author aaa
 * @since 2020-12-03
 */
@RestController
@RequestMapping("/outbound/pick-detail")
public class PickDetailController {
    @Autowired
    private PickDetailService pickDetailService;
    @Autowired
    private PickService pickService;
    @Autowired
    private OutorderService outorderService;

    @PostMapping("addPickDetail/{pickno}/{commoditybar}/{locatorcode}/{qty}")
    @ApiOperation("录入拣货明细")
    private R addPickDetail(@ApiParam(value = "拣货编号")@PathVariable String pickno,
                            @ApiParam(value = "商品编码")@PathVariable String commoditybar,
                            @ApiParam(value = "货位编码")@PathVariable String locatorcode,
                            @ApiParam(value = "拣货编号")@PathVariable Integer qty){
        QueryWrapper<PickDetail> queryWrapper1=new QueryWrapper<>();
        QueryWrapper<Pick> queryWrapper2=new QueryWrapper<>();
        queryWrapper2.eq("pickno",pickno);
        Pick pick1 = pickService.getOne(queryWrapper2);
        Long pid = pick1.getId();
        queryWrapper1.eq("commoditybar",commoditybar);
        queryWrapper1.eq("locatorcode",locatorcode);
        queryWrapper1.eq("pid",pid);
        PickDetail one = pickDetailService.getOne(queryWrapper1);
        if(one!=null){
            return R.error().message("请勿重复录入");
        }
        List<PicktaskDetail> picktaskDetails = pickDetailService.selectPicktaskDetailByPickno(pickno);
        QueryWrapper<Pick> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("pickno",pickno);
        Pick pick = pickService.getOne(queryWrapper);
        for(PicktaskDetail picktaskDetail:picktaskDetails){
            if(picktaskDetail.getCommoditybar().equals(commoditybar)){
                if(picktaskDetail.getLocatorcode().equals(locatorcode)){
                    if(picktaskDetail.getQty()==qty){
                        PickDetail pickDetail=new PickDetail();
                        pickDetail.setPid(pick.getId());
                        pickDetail.setCommodityid(picktaskDetail.getCommodityid());
                        pickDetail.setCommoditybar(picktaskDetail.getCommoditybar());
                        pickDetail.setCommodityname(picktaskDetail.getCommodityname());
                        pickDetail.setLocatorid(picktaskDetail.getLocatorid());
                        pickDetail.setLocatorcode(picktaskDetail.getLocatorcode());
                        pickDetail.setLocatorname(picktaskDetail.getLocatorname());
                        pickDetail.setQty(picktaskDetail.getQty());
                        Date now = new Date();
                        pickDetail.setCreatetime(now);
                        pickDetailService.save(pickDetail);
                        pick.setState("1");
                        pickService.updateById(pick);
                        return R.ok().message("录入成功").code(200);
                    }else {
                        continue;
                        //return R.error().message("商品数量不匹配");
                    }
                }else {
                    continue;
                    //return R.error().message("货位编码不匹配");
                }
            }else {
                continue;
                //return R.error().message("商品编码不匹配");
            }
        }
        return R.error().message("商品信息不匹配");
    }

    @PostMapping("endWritePick/{pickno}")
    @ApiOperation(value = "结束录入拣货明细")
    public R endWritePick(@ApiParam(value = "拣货单号")@PathVariable("pickno") String pickno){
        QueryWrapper<Pick> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("pickno",pickno);
        Pick pick = pickService.getOne(queryWrapper);
        pick.setState("2");
        pickService.updateById(pick);
        return R.ok().message("录入明细成功").code(200);
    }

    @PostMapping("viewPick/{pickno}")
    @ApiOperation(value = "查看拣货明细")
    public R viewPick(@ApiParam(value = "拣货单号")@PathVariable("pickno") String pickno){
        List<PickDetail> pickDetails = pickDetailService.selectPickDetailByPickno(pickno);
        return R.ok().data("pickDetail",pickDetails).code(200);
    }


    @PostMapping("returnPick/{pickno}")
    @ApiOperation(value = "退还拣货")
    public R returnPick(@ApiParam(value = "拣货单号")@PathVariable("pickno") String pickno){
        Long oid = pickDetailService.selectOutorderIdByPickno(pickno);
        Outorder outorder = outorderService.getById(oid);
        if(!outorder.getState().equals("")) {
            if (!outorder.getState().equals("5")) {
                QueryWrapper<Pick> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("pickno", pickno);
                Pick pick = pickService.getOne(queryWrapper);
                Long pid = pick.getId();
                QueryWrapper<PickDetail> queryWrapper1 = new QueryWrapper<>();
                pickDetailService.remove(queryWrapper1);
                pickService.removeById(pid);
                outorder.setState("3");
                outorderService.updateById(outorder);
                return R.ok().message("退还成功").code(200);
            } else {
                return R.error().message("该订单已出库");
            }
        }else {
            return R.error().message("该订单异常");
        }
    }

    @PostMapping("searchCommodityByPicktaskno/{picktaskno}")
    @ApiOperation(value = "根据拣货任务单号获取商品信息")
    public R searchCommodityByPicktaskno(@ApiParam(value = "拣货任务单号") @PathVariable("picktaskno") String picktaskno){
        List<PicktaskDetail> picktaskDetails = pickDetailService.searchCommodityByPicktaskno(picktaskno);
        return R.ok().code(200).data("commoditys",picktaskDetails);
    }
    @PostMapping("searchLocatorByCommodity/{picktaskno}/{commoditybar}")
    @ApiOperation(value = "根据拣货任务单号和商品编码获取库位信息")
    public R searchLocatorByCommodity(@ApiParam(value = "拣货任务单号") @PathVariable("picktaskno") String picktaskno,
                                      @ApiParam(value = "商品编码") @PathVariable("commoditybar") String commoditybar){
        List<PickDetail> pickDetails = pickDetailService.searchLocatorByCommodity(picktaskno, commoditybar);
        return R.ok().code(200).data("locators",pickDetails);
    }




}

